package com.laoniu.service.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laoniu.dto.request.BaseSupplierRequestDto;
import com.laoniu.entity.BaseSupplier;
import com.laoniu.entity.SecurityUser;
import com.laoniu.mapper.BaseSupplierMapper;
import com.laoniu.utils.SpringSecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 供应商表 服务实现类
 * </p>
 *
 * @author lxl
 * @since 2023-12-17
 */
@Service
public class BaseSupplierService extends ServiceImpl<BaseSupplierMapper, BaseSupplier> {

    @Resource
    private BaseSupplierMapper supplierMapper;

    /**
     * 分页查询供应商列表
     * @param requestDto
     * @return
     */
    public Page<BaseSupplier> listPage(BaseSupplierRequestDto requestDto) {
        SecurityUser user = SpringSecurityUtils.getUser();
        Page<BaseSupplier> page = new Page<>(requestDto.getPageNo(), requestDto.getPageSize());
        LambdaQueryWrapper<BaseSupplier> wrapper = Wrappers.lambdaQuery(BaseSupplier.class);
        wrapper.like(StringUtils.isNotBlank(requestDto.getName()), BaseSupplier::getName, requestDto.getName());
        if (!user.isAdmin()){
            wrapper.eq(BaseSupplier::getOpId, user.getId());
        }
        wrapper.orderByDesc(BaseSupplier::getId);
        return supplierMapper.selectPage(page, wrapper);
    }
}
